package com.yuexun.beilunpatient.ui.doctor.ui.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.doctor.adapter.DoctorListAdapter;
import com.yuexun.beilunpatient.ui.doctor.bean.CollectionDocInfoBean;
import com.yuexun.beilunpatient.ui.doctor.model.impl.DoctorModel;
import com.yuexun.beilunpatient.ui.doctor.presenter.impl.CollectionDoctorPresenter;
import com.yuexun.beilunpatient.ui.doctor.presenter.impl.DeleteCollectionPresenter;
import com.yuexun.beilunpatient.ui.doctor.ui.view.ICollectionDoctorView;
import com.yuexun.beilunpatient.ui.doctor.ui.view.IDeleteCollectionView;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/22 0022.
 */

public class Frag_Doc_Collect extends KJFragment implements ICollectionDoctorView, IDeleteCollectionView {
    @Bind(R.id.content_list)
    PullToRefreshList contentList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private ListView mList;
    DoctorListAdapter adapter;
    private int nowpage = 1;
    private int pagesize = 20;
    CollectionDoctorPresenter presenter;
    List<Object> datas = new ArrayList<>();

    DeleteCollectionPresenter deletePresenter;
    CollectionDocInfoBean item;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        presenter = new CollectionDoctorPresenter(this, new DoctorModel());
        deletePresenter = new DeleteCollectionPresenter(this, new DoctorModel());
        ApiGetData();
        listViewPreference();
    }

    /**
     * 请请求数据
     */
    private void ApiGetData() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage + "");
        params.put("pageSize", pagesize + "");
        presenter.inquireCollectionDoctorPageList(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = contentList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        contentList.setPullRefreshEnabled(true);
        contentList.setPullLoadEnabled(true);
        ((FooterLoadingLayout) contentList.getFooterLoadingLayout())
                .setNoMoreDataText("已经是最新状态");

        adapter = new DoctorListAdapter(mList, datas, R.layout.item_doctor);
        mList.setAdapter(adapter);

        adapter.setListener(new ICallBackListener() {
            @Override
            public void callback(Bundle bundle) {
                item = (CollectionDocInfoBean) datas.get(bundle.getInt("position"));
                Map<String, String> params = new HashMap<>();
                params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
                params.put("slHospKey", item.getSlHospKey());
                params.put("jobNum", item.getJobNum());
                deletePresenter.deleteCollectionDoctor(params);
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
//                startActivity(new Intent(getActivity(), Act_Doc_Detail.class));
            }
        });

        contentList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiGetData();
            }

            @Override
            public void onPullUpToRefresh(
                    PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiGetData();
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiGetData();
            }
        });
    }

    @Override
    public void getCollectionDoctorList(BaseEntity<CollectionDocInfoBean> bean) {
        if (contentList.isPullRefreshEnabled()) {
            contentList.onPullDownRefreshComplete();
        }
        if (bean.getStatus().equals("200")) {
            if (bean.getDatas().size() == 0) {
                contentList.setHasMoreData(false);
            } else {
                if (contentList.isPullLoadEnabled()) {
                    contentList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                datas.clear();
            }
            datas.addAll(bean.getDatas());
            adapter.refresh(datas);

        }else {
            ViewInject.toast(bean.getMessage());
        }

        if (datas.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

    @Override
    public void deleteCollectionDoctor(BaseEntity<String> bean) {
        if(bean.getStatus().equals("200")){
            datas.remove(item);
            adapter.notifyDataSetInvalidated();
        }
    }
}
